package tetrismain;

import java.util.ArrayList;
import java.util.Random;

public class Board {
	public static int DEFAULT_WIDTH = 10;
	public static int DEFAULT_HEIGHT = 20;
	private ArrayList<Row> my_blocks;
	private Piece currentPiece;
	private boolean game_over;

	public Board() {
		game_over = false;
		my_blocks = new ArrayList<Row>();
		for (int i = 0; i <= DEFAULT_HEIGHT; i++) {
			my_blocks.add(i, new Row());
		}
		currentPiece = nextPiece();
	}

	public ArrayList<Block> getBlocks() {
		ArrayList<Block> copy = new ArrayList<Block>();
		for (Row i : my_blocks) {
			for (Block b : i.my_blocks) {
				copy.add(new Block(b.getX(), b.getY(), b.getId()));
			}
		}
		return copy;
	}

	public boolean gameOver() {
		return game_over;
	}

	public Piece nextPiece() {
		Piece nextPiece;
		Random r = new Random();
		int x = r.nextInt(7);
		if (x == Piece.I_ID - 1) {
			nextPiece = new IPiece();
		} else if (x == Piece.J_ID - 1) {
			nextPiece = new JPiece();
		} else if (x == Piece.L_ID - 1) {
			nextPiece = new LPiece();
		} else if (x == Piece.O_ID - 1) {
			nextPiece = new OPiece();
		} else if (x == Piece.S_ID - 1) {
			nextPiece = new SPiece();
		} else if (x == Piece.T_ID - 1) {
			nextPiece = new TPiece();
		} else {
			nextPiece = new ZPiece();
		}
		return nextPiece;
	}

	public Piece getCurentPiece() {
		return currentPiece;
	}

	public void update() {
		currentPiece.moveDown();
	}

	public void moveDown() {
		boolean move = true;
		Block c;
		for (Block i : currentPiece.getPiece()) {
			c = new Block(i.getX(), i.getY() + 1, i.getId());
			if (i.getY() >= DEFAULT_HEIGHT) {
				move = false;
				break;
			} else if (my_blocks.get(c.getY()).my_blocks.contains(c)) {
				move = false;
				break;
			}
		}
		if (move == true) {
			currentPiece.moveDown();
		} else {
			addPiece();
			currentPiece = nextPiece();
		}
	}

	public void addPiece() {
		for (Block i : currentPiece.getPiece()) {
			if (i.getY() == 0) {
				game_over = true;
			}
			Row c = my_blocks.get(i.getY());
			c.my_blocks.add(i);
		}
		this.clearRows();
	}

	public void moveLeft() {
		boolean move = true;
		Block c;
		for (Block i : currentPiece.getPiece()) {
			c = new Block(i.getX() - 1, i.getY(), i.getId());
			if (my_blocks.get(c.getY()).my_blocks.contains(c)) {
				move = false;
				break;
			} else if (i.getX() <= 0) {
				move = false;
				break;
			}
		}
		if (move == true) {
			currentPiece.moveLeft();
		}

	}

	public void moveRight() {
		boolean move = true;
		Block c;
		for (Block i : currentPiece.getPiece()) {
			c = new Block(i.getX() + 1, i.getY(), i.getId());
			if (my_blocks.get(c.getY()).my_blocks.contains(c)) {
				move = false;
				break;
			} else if (i.getX() >= DEFAULT_WIDTH) {
				move = false;
				break;
			}
		}
		if (move == true) {
			currentPiece.moveRight();
		}
	}

	public void clearRows() {
		ArrayList<Row> full_rows = new ArrayList<Row>();
		for (Row r : my_blocks) {
			if (r.my_blocks.size() >= DEFAULT_WIDTH - 1) {
				full_rows.add(r);
			}
		}
		int rn;
		for (Row r : full_rows) {
			rn = my_blocks.indexOf(r);
			my_blocks.remove(r);
			moveDown(rn);
			my_blocks.add(new Row());
		}
	}

	private void moveDown(int row) {
		System.out.print(row);
		Row r;
		for (int i = 0; i < row; i++) {
			r = my_blocks.get(i);
			for (Block b : r.my_blocks) {
				b.setY(b.getY() + 1);
			}
		}
	}

	public void rotate() {
		currentPiece.rotate();
		boolean rotate = true;
		for (Block i : currentPiece.getPiece()) {
			if (my_blocks.get(i.getY()).my_blocks.contains(i)) {
				rotate = false;
				break;
			} else if (i.getX() >= DEFAULT_WIDTH) {
				rotate = false;
				break;
			} else if (i.getX() < 0) {
				rotate = false;
				break;
			} else if (i.getY() >= DEFAULT_HEIGHT) {
				rotate = false;
				break;
			} else if (i.getY() < 0) {
				rotate = false;
				break;
			} else {
				rotate = true;
			}
		}
		if (rotate == false) {
			currentPiece.rotate();
			currentPiece.rotate();
			currentPiece.rotate();
		}
	}

	private class Row {
		private ArrayList<Block> my_blocks;

		public Row() {
			my_blocks = new ArrayList<Block>();
		}
	}
}
